home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / comms / other / atomic2_0 / atomic.rexx < prev    next >
OS/2 REXX Batch file  |  1999-06-14  |  12KB  |  588 lines

  1. /*     * * * * *    A T O M I C . R E X X     * * * * *           */
  2. /*                                                                */
  3. /*      file: s:atomic.rexx                                       */
  4. /*                                                                */
  5. /*      by John Chufar                                            */
  6. /*      ver 2.0.2  13 Apr 1999                                    */
  7. /*      Enhancements:                                             */
  8. /*      -IntelliServe Added                                       */
  9. /*      -Added Logging module                                     */
  10. /*      -Added Configuration module                               */
  11. /*                                                                */
  12. /*                                                                */
  13. /* ************************************************************** */
  14.  
  15.  
  16.  
  17. startup:
  18.  
  19. /* init the command channel to AmigaDOS and see if TCP is mounted */
  20.  
  21. ADDRESS COMMAND
  22.  
  23. " Assign >NIL: TCP: exists "
  24.  
  25. IF RC = 5 THEN
  26.    DO
  27.       SAY ' TCP: not mounted ...read docs please... exiting  '
  28.       SAY ''
  29.       EXIT 10
  30.    END
  31.  
  32. startupend:
  33.  
  34.  
  35.  
  36. debug:
  37.  
  38. /* Use TRACE only for debug--it is very time consuming! */
  39. /* TRACE(I)  */
  40.  
  41. debugend:
  42.  
  43.  
  44.  
  45. titlescreen:
  46.  
  47. /* display the title screen ...   */
  48.  
  49. SAY ''
  50. SAY ''
  51. SAY '                 * * *   A T O M I C L O C K  * * *         '
  52. SAY '                                                            '
  53. SAY '                             Version 2.0.2                    '
  54. SAY '                          1997, 1998, 1999                  '
  55. SAY '                  "intelliserve" (TM) Technology ~ iT       '
  56. SAY ''
  57. SAY ''
  58.  
  59.  
  60. /* argstring$ set module */
  61.  
  62. argstring$ = ''
  63.  
  64. numargs = ARG()  /* number of args */
  65.  
  66. IF numargs > 0 THEN ARG argstring$
  67.  
  68. titlescreenend:
  69.  
  70.  
  71.  
  72. /* defaults          */
  73.  
  74. servercount = 0
  75. TIMEAVG. = 0.1
  76.  
  77.  
  78.  
  79. readconfig:
  80.  
  81.  
  82. IF ~OPEN('cf','s:atomic.conf', 'R')
  83.    THEN DO
  84.       Say '*** Unable to open configuration file, atomic.conf'
  85.       Exit 10
  86.    END
  87.  
  88. DO WHILE ~EOF('cf')
  89.    instring = READLN('cf')
  90.    INTERPRET instring
  91.    IF (INDEX(instring,'TIMESERVER') =1)
  92.       THEN DO
  93.       servercount = servercount + 1
  94.    END
  95.   END
  96.  
  97.  
  98. clcode = CLOSE('cf')
  99.  
  100.  
  101.  
  102. readlog:
  103.  
  104.  
  105. IF ~OPEN('lf',LOGFILE, 'R')
  106.    THEN DO
  107.       Say '*** Unable to open log file, ' LOGFILE
  108.       Exit 10
  109.    END
  110.  
  111.  
  112.    SHOWAVG = '0'
  113.    IF (AVG='YES') THEN SHOWAVG ='1'
  114.  
  115.    CMPA = 0
  116.    CMPA = INDEX(argstring$,'A')
  117.  
  118.    IF CMPA > 0
  119.       THEN DO
  120.          SHOWAVG = '1'
  121.       END
  122.  
  123.  
  124.    DO i=1 TO servercount
  125.       c=0
  126.       timesum=0
  127.       DO WHILE ~EOF('lf')
  128.        logstring = READLN('lf')
  129.        IF (INDEX(logstring,TIMESERVER.i) =1)
  130.          THEN DO
  131.             c = c + 1
  132.             TIMESUM.c = WORD(logstring,2)
  133.             timesum= TIMESUM.c + timesum
  134.          END
  135.       END
  136.       seekcode = SEEK('lf',0,'B')
  137.       IF (c > 0)
  138.          THEN DO
  139.            TIMEAVG.i = timesum / c
  140.          END
  141.  
  142.        IF SHOWAVG > 0
  143.          THEN DO
  144.            AVGOUT$ = 'AVERAGE CONNECT TIME FOR 'TIMESERVER.i': '
  145.            SAY OVERLAY(TIMEAVG.i,AVGOUT$,65,,'.')
  146.          END
  147.    END
  148.    SAY ''
  149.  
  150. clcode = CLOSE('lf')
  151.  
  152.  
  153.  
  154. sortdata:
  155.  
  156. /* bubble sort the server data */
  157.  
  158.  
  159. loop = servercount - 1
  160.  
  161. DO j = 1 to loop
  162.    k = j+1
  163.    DO l = k to servercount
  164.       IF TIMEAVG.j > TIMEAVG.l
  165.             THEN DO
  166.                tempavg = TIMEAVG.j ; tempserv = TIMESERVER.j
  167.                TIMEAVG.j = TIMEAVG.l; TIMESERVER.j = TIMESERVER.l
  168.                TIMEAVG.l = tempavg; TIMESERVER.l = tempserv
  169.             END
  170.    END
  171.  
  172. END
  173.  
  174.  
  175. IF ~OPEN('sf','t:sortfile', 'W')
  176.    THEN DO
  177.       Say '*** Unable to open temp sort file, t:sortfile'
  178.       Exit 10
  179.    END
  180.  
  181.    DO i=1 TO servercount
  182.       outstring = TIMEAVG.i||' '||TIMESERVER.i
  183.       wrcode = WRITELN('sf',outstring)
  184.    END
  185.  
  186. clcode = CLOSE('sf')
  187.  
  188.  
  189.  
  190. logfile:
  191.  
  192. /* open the log file to append  */
  193.  
  194. IF ~Open('lf',LOGFILE, 'A')
  195.    THEN DO
  196.       Say '*** Unable to append to log file'
  197.       Exit 10
  198.    END
  199.  
  200.  
  201.  
  202. /* open the tcp channel to the host */
  203.  
  204. IF ~OPEN('sr','t:sortfile', 'R')
  205.    THEN DO
  206.       Say '*** Unable to open temp sortfile'
  207.       Exit 10
  208.    END
  209.  
  210.  
  211.  
  212. connect:
  213.  
  214.  
  215. serverstart = TIME('E')
  216.  
  217. IF ~EOF('sr')
  218.          THEN DO
  219.             serverstring = READLN('sr')
  220.             intelliserver = WORD(serverstring,2)
  221.             intelliport = 'TCP:'||intelliserver||'/daytime'
  222.          END
  223.          ELSE DO
  224.             SAY '*** NIST Time Services are not present...exiting'
  225.             SAY ''
  226.             SAY 'Try again at a later time.'
  227.             SAY '**************************************'
  228.             EXIT 10
  229.          END
  230.  
  231.  
  232. SAY 'Connecting to Atomic Clock server 'intelliserver
  233.  
  234. IF ~OPEN(dt,intelliport, 'R')
  235.    THEN DO
  236.       SAY 'Connection error with 'intelliserver
  237.       serverend = TIME('E')
  238.       logdaystring = DATE()
  239.       logtimestring = TIME()
  240.       serverelapsed = serverend - serverstart
  241.       serverlog = intelliserver || "   " || serverelapsed ||"   "|| logdaystring ||" "||logtimestring
  242.  
  243.       wrcode = WRITELN('lf',serverlog)
  244.       CALL connect
  245.    END
  246.  
  247.  
  248. serverend = TIME('E')
  249. serverelapsed = serverend - serverstart
  250. SAY 'Server connection time: ' serverelapsed
  251.  
  252.  
  253.  
  254. connectend:
  255.  
  256.  
  257.  
  258. decode:
  259. /* get the atomic time from the open file 'dt'  */
  260.   
  261. d = readln(dt) /* dummy read...empty first line */
  262. d = readln(dt)
  263.  
  264. serverhealth = WORD(d,6)
  265. utcdst = WORD(d,4)
  266.  
  267. IF (serverhealth ~= 0)
  268.    THEN DO
  269.       SAY "Server Health: " serverhealth " insufficient...trying next server."
  270.       SAY ''
  271.       CALL connect
  272.    END
  273.  
  274. SAY "Server Health: " serverhealth " (Excellent)"
  275.  
  276.  
  277. tim = SubWord(d, 3, 1)
  278.  
  279. utcdate=SubWord(d,2,1)
  280.  
  281. utcday=SubStr(utcdate,7,2)
  282. utcmonth=SubStr(utcdate,4,2)
  283. utcyear=SubStr(utcdate,1,2)
  284.  
  285. decodend:
  286.  
  287.  
  288.  
  289. oldtime = TIME('N')
  290.  
  291. /* changed in 1.5 from DATE('B') */
  292.  
  293. olddate = DATE('I')
  294.  
  295. Date TIME tim DATE utcday'-'utcmonth'-'utcyear
  296.  
  297.  
  298.  
  299. calculate_timezone:
  300.  
  301.  
  302. /*      Script to offset the time to local                        */
  303. /*      and calculates your localtime 'error'                     */
  304.  
  305. IF ~(Exists('env:sys/locale.prefs'))
  306.    THEN DO
  307.      Say 'Fatal error *** Env:sys/locale.prefs does not exist ***'
  308.      Exit 10
  309.    END
  310.  
  311. IF ~Open(pf, 'env:sys/locale.prefs', 'R')
  312.    THEN DO
  313.       SAY '*** Unable to open prefs file'
  314.       Exit 10
  315.    END
  316.  
  317. dummy = SEEK(pf,384,'B')
  318.  
  319. d = readch(pf,2)
  320. tzoval = C2D(d)
  321.  
  322. /* tzoval is the timezone offset in minutes */
  323.  
  324. gmtflag = 0
  325.  
  326. IF tzoval > 660      /* if the timeszone is > international date line */
  327.    THEN DO
  328.    tzoval = 65536 - tzoval       /* reverse offset (12 hours or greater */
  329.    tzocalc = tzoval/60           /* convert to hours.decimins */
  330.    tzocalc = TRUNC(tzocalc,2)    /* hours.mm format */
  331.    tzohour = TRUNC(tzocalc)      /* hours */
  332.    tzomin  = RIGHT(tzocalc,2)    /* extract minutes */
  333.    tzomin  = tzomin * .6         /* convert decimal to minutes */
  334.    tzomin  = TRUNC(tzomin)       /* discard decimals (seconds) */
  335.    tzohour = 24 - tzohour        /* complete the offset */
  336.    if tzomin > 0 THEN tzohour = tzohour -1
  337.    gmtflag = 1
  338.    END
  339.  
  340.  
  341. IF gmtflag = 0      /* if the timezone is < 12 hours fom GMT */
  342.    THEN DO
  343.    tzocalc = tzoval/60           /* convert to hours.decimins */
  344.    tzocalc = TRUNC(tzocalc,2)    /* hours.mm format */
  345.    tzohour = TRUNC(tzocalc)      /* hours */
  346.    tzomin  = RIGHT(tzocalc,2)    /* extract minutes */
  347.    tzomin  = tzomin * .6         /* convert decimal to minutes */
  348.    tzomin  = TRUNC(tzomin)       /* discard decimals (seconds) */
  349.    END
  350.  
  351.  
  352. IF (UTCDST = '00') | (UTCDST >'50') THEN TIMECODE = 'STANDARD'
  353. IF (UTCDST >'00') & (UTCDST <'51') THEN TIMECODE = 'DAYLIGHT'
  354.  
  355. /* if user enters y or n, this overrides the DST in config file */
  356.  
  357. CMPY = INDEX(argstring$,'Y')
  358.  
  359. IF CMPY > 0
  360.    THEN DO
  361.       TIMECODE = 'DAYLIGHT'
  362.    END
  363.  
  364. CMPN = INDEX(argstring$,'N')
  365.  
  366. IF CMPN > 0
  367.    THEN DO
  368.       TIMECODE = 'STANDARD'
  369.    END
  370.  
  371.  
  372. IF (TIMECODE = 'DAYLIGHT')
  373.    THEN DO
  374.       tzohour = tzohour -1
  375.    END
  376.  
  377.  
  378. tzoff$ = "-" || tzohour
  379.  
  380. IF tzohour > 12
  381.    THEN DO
  382.    tzoff = 24 - tzohour
  383.    IF tzomin > 0 THEN tzoff = tzoff -1  /* Compensate for half hour zones */
  384.    tzoff$ = "+" || tzoff
  385.    END
  386.  
  387. SAY 'Time adjusted '|| tzoff$ ||' hours and ' tzomin ' minutes from UTC.'
  388. SAY ''
  389.  
  390.  
  391. /* read current system time and extract hours and minutes */
  392.  
  393. timestring = TIME('N')
  394. mins = SUBSTR(timestring,4,2)
  395. hour = LEFT(timestring,2)
  396.  
  397. newmins = mins - tzomin
  398. IF newmins < 0
  399.    THEN DO
  400.    newmins = newmins + 60        /* positive value for minutes */
  401.    tzohour = tzohour + 1         /* borrow an hour for substraction */
  402.    END
  403.  
  404.  
  405. offset = tzohour  /* set for Your Time Zone */
  406. localhour = hour - offset
  407.  
  408. minusday = 0
  409. IF localhour <0
  410.  THEN  DO
  411.      localhour = localhour + 24
  412.      minusday = 1
  413.  END
  414.  
  415. newhour = localhour
  416.  
  417.  
  418. /* correct for hours and minutes of one character '<10'  */
  419.  
  420. IF localhour <10 THEN newhour = "0" || localhour
  421. IF newmins < 10 THEN newmins = "0" || newmins
  422.  
  423.  
  424. /* create the new time string */
  425.  
  426. newtime = newhour || ":" || newmins ||SUBSTR(timestring,6,3)
  427.  
  428.  
  429. /* Correct for the date  */
  430. IF (minusday = 0) & (gmtflag = 1) THEN 'DATE TOMORROW'
  431. IF (minusday = 1) & (gmtflag = 0) THEN 'DATE YESTERDAY'
  432. /* ELSE leave the data as TODAY */
  433.  
  434.  
  435. /*  AmigaDOS DATE commands  */
  436.  
  437. 'DATE TIME ' || newtime
  438.  
  439. 'DATE'
  440.  
  441. 'SetClock SAVE'
  442. SAY ''
  443.  
  444.  
  445. /* changed in 1.5 from DATE('B')  */
  446. newdate = DATE('I')
  447.  
  448.  
  449.  
  450. /* Compensation for initial clock read                 */
  451. /* Adjust by adding 1 second due to program exec time  */
  452. /* Average exec time was .56 seconds on A3000 25MHz    */
  453.  
  454. adjustment:
  455.  
  456. adjsec = RIGHT(oldtime,2)
  457. adjmin = SUBSTR(oldtime,4,2)
  458. adjhour = LEFT(oldtime,2)
  459.  
  460. adjsec = adjsec + 1
  461.  
  462.    IF adjsec > 59 THEN
  463.       DO
  464.           adjsec = 0
  465.           adjmin = adjmin + 1
  466.             IF adjmin > 60 THEN
  467.                DO
  468.                   adjmin = 0
  469.                   adjhour = adjhour + 1
  470.                      IF adjhour > 23 THEN
  471.                         DO
  472.                            adjhour = 0
  473.                            olddate = olddate + 1
  474.                          END
  475.                END
  476.       END
  477.  
  478. /* ensures values are converted to integer types */
  479.  
  480. adjsec = adjsec + 0
  481. adjmin = adjmin + 0
  482. adjhour = adjhour + 0
  483.  
  484.  
  485.  
  486. adjsecstr$  = adjsec
  487. IF adjsecstr$ = '0' THEN adjsecstr$ = '00'
  488. IF (adjsec > 0 ) & (adjsec < 10) THEN adjsecstr$ = '0' || adjsecstr$
  489.  
  490. adjminstr$ = adjmin
  491. IF adjminstr$ = '0' THEN adjminstr$ = '00'
  492. IF (adjmin > 0 ) & (adjmin < 10) THEN adjminstr$ = '0' || adjminstr$
  493.  
  494. adjhourstr$ = adjhour
  495. IF adjhourstr$ = '0' THEN adjhourstr$ = '00'
  496. IF (adjhour > 0 ) & (adjhour < 10) THEN adjhourstr$ = '0' || adjhourstr$
  497.  
  498. oldtime = adjhourstr$||':'||adjminstr$||':'||adjsecstr$
  499.  
  500. adjustmentend:
  501.  
  502.  
  503.  
  504. oldtimesecs = timeseccalc(oldtime)
  505. newtimesecs = timeseccalc(newtime)
  506.  
  507.  
  508. SAY ' Previous System time was: ' oldtime
  509. SAY ' Current System time is  : ' newtime
  510.  
  511. oldsecs =  (olddate *  86400) + oldtimesecs
  512. newsecs =  (newdate * 86400)  + newtimesecs
  513.  
  514.  
  515. secdiff = newsecs - oldsecs
  516.  
  517. secdiff  =  ABS(secdiff)
  518.  
  519. datediff = TRUNC(secdiff / 86400)
  520.  
  521. compsecdiff = secdiff - (datediff * 86400)
  522.  
  523. diffstr$ = timediff(compsecdiff)
  524.  
  525. /* datediff = newdate - olddate */
  526. /* datediff = ABS(datediff)     */
  527.  
  528. SAY ''
  529. SAY ' Time difference was ' datediff ' days and 'diffstr$
  530. SAY ''
  531.  
  532. logdiff$ = "  "||datediff||" days  "||diffstr$
  533.  
  534. /* log successful connection information */
  535.  
  536. logdaystring = DATE()
  537. logtimestring = TIME()
  538.  
  539. serverlog = intelliserver || "   " || serverelapsed ||"   "|| logdaystring ||"   "||logtimestring||logdiff$
  540. wrcode = WRITELN('lf',serverlog)
  541.  
  542. clcode =  CLOSE('lf')
  543.  
  544.  
  545. EXIT
  546.  
  547.  
  548. /* ******************  TIME CALC  MODULE   **************   */
  549.  
  550. /* Function to calc timesecs */
  551. timeseccalc: procedure
  552.  ARG timestring
  553.  
  554.  SECS = RIGHT(timestring,2)
  555.  MINS = SUBSTR(timestring,4,2)
  556.  HOURS = LEFT(timestring,2)
  557.  
  558. timesecs = (HOURS * 3600) + (MINS * 60) + (SECS)
  559.  
  560. RETURN timesecs
  561.  
  562.  
  563. /* Function to converts seconds into HH:MM:SS format */
  564. timediff: procedure
  565.  ARG timenumsecs
  566.  
  567.    hours = timenumsecs % 3600
  568.    hoursecs = hours * 3600
  569.    mins = (timenumsecs - (hoursecs)) % 60
  570.    minsecs = mins * 60
  571.    secs = timenumsecs - hoursecs - minsecs
  572.  
  573.    hourstr$ = hours
  574.    IF (hours < 10) THEN hourstr$ = '0' || hours
  575.    minstr$ = mins
  576.    IF (mins < 10)  THEN minstr$ = '0' || mins
  577.    secstr$ = secs
  578.    IF (secs < 10)  THEN secstr$ = '0' || secs
  579.  
  580.    timeconvstr$ = hourstr$||':'||minstr$||':'||secstr$
  581.  
  582.  
  583.  RETURN timeconvstr$
  584.  
  585.  
  586.  
  587. /* ****************   E N D   O F   P R O G R A M   ****************** */
  588.